package mdr;

import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Collection;
import model.TestClusterBuilder;
import model.StatementKind;
import model.Filter;
import util.GAConfig;
public class MethodDependences {
	private final MethodRelations relations;

	public MethodDependences(Collection<Class<?>> cluster) {
		relations = new MethodRelations(cluster);
		//this.buildDependence();
	}

	public void buildDependence(List<StatementKind> models) {
		try {
			relations.buildRelations(models);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public MethodRelations getRelations() {
		return this.relations;
	}

	public String showDependence(Set<Class<?>> classToTest) {
		return this.relations.showDependence( classToTest);
	}

	public String showStatmentsWriteFileds(Set<Class<?>> classToTest) {
		return this.relations.showStatmentsWriteFileds(classToTest);
	}
	public String showFieldReadWrites(Set<Class<?>> classToTest) {
		return this.relations.showFieldReadWrites( classToTest);
	}
	public String showDependenceRelevance(Set<Class<?>> classToTest) {
		return this.relations.showDependenceRelevance( classToTest);
	}
	public static void main(String[] args) throws ClassNotFoundException {
		Set<Class<?>> allClasses = new LinkedHashSet<Class<?>>();

		allClasses.addAll(TestClusterBuilder.readClassFromFile());

		System.out.println("There are " + allClasses.size()
				+ " classes as input");

		System.out.println(allClasses);
		List<TestClusterBuilder> TestClusterList = TestClusterBuilder.buildTestClusterList(allClasses);
		for (TestClusterBuilder tCluster : TestClusterList) {
			Set<Class<?>> classToTest= new LinkedHashSet<Class<?>>();

			Map<String, Class<?>> classesToTest = tCluster.getClassToTest();
			String cTName = classesToTest.keySet().iterator().next();
			classToTest.add(classesToTest.get(cTName));
		
			System.out.println("Class " + cTName + " is being analyzed");
			 Set<Class<?>> cluster = new LinkedHashSet<Class<?>>();
				for(String cName:tCluster.getCluster().keySet()){
					Class<?> cls=tCluster.getCluster().get(cName).getClazz();
					if(Filter.isPrimitiveOrStringType(cls))continue;
					if(cls.getName().startsWith("java.")) continue;
					if(cls.getName().startsWith("sun.")) continue;
					if(cls.getName().contains("$")) continue;
					if(!Filter.canUse(cls))continue;
					cluster.add(cls);
				}
				MethodDependences recommender = new MethodDependences(cluster);
				 recommender.buildDependence(tCluster.getStatements());
				 System.out.println("Finishing method dependence model building");
				 
				 ////print out the relation methods dependency

		//			for(Class<?> cls: tCluster.getClassToTest().values())

				System.out.println("--------------------");
				System.out.println("     CUT: "+ classToTest);
				System.out.println("--------------------");
				System.out.println(recommender.showFieldReadWrites(classToTest));
				System.out.println(recommender.showStatmentsWriteFileds(classToTest));
								
			
				
				
				if(!GAConfig.use_tf_idf)
					System.out.println(recommender.showDependence(classToTest));
				else
					System.out.println(recommender.showDependenceRelevance(classToTest));

		}

		System.out.println();
		System.exit(0);
	}

}
